package main

import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)

func main() {
	src := rand.NewSource(time.Now().UnixNano())
	r := rand.New(src)

	// 生成100个[-100, 100]范围内的随机整数
	nums := make([]int, 100)
	for i := range nums {
		nums[i] = r.Intn(201) - 100
	}

	// 统计每个数字出现的次数
	countMap := make(map[int]int)
	for _, num := range nums {
		countMap[num]++
	}

	// 将map中的键值对转换为切片，并按值降序排序
	countSlice := make([]struct {
		num   int
		count int
	}, 0, len(countMap))
	for num, count := range countMap {
		countSlice = append(countSlice, struct {
			num   int
			count int
		}{num, count})
	}
	sort.Slice(countSlice, func(i, j int) bool {
		return countSlice[i].count > countSlice[j].count
	})

	// 打印结果
	for _, item := range countSlice {
		fmt.Printf("%d: %d\n", item.num, item.count)
	}
}
